<?php
/*
 * @开发工具: JetBrains PhpStorm.
 * @文件名：UserLoan.class.php
 * @类功能: 用户贷款信用控制器
 * @开发者: gys
 * @开发时间： 2018-04-19
 * @版本：version 1.0
 */

namespace app\api\controller;

use app\common\controller\ApiBaseController;
use app\common\enum\DatumStatus;
use app\common\enum\HtmlEnumValue;
use app\common\enum\IsDelete;
use app\common\enum\IsHistory;
use app\common\enum\IsLoan;
use app\common\enum\LoanType;
use app\common\ext\IDb;
use app\common\ext\User;

class UserLoan extends ApiBaseController{
    /**
     * @功能：个人贷款列表
     * @开发者：gys
     */
    public function loan_list(){
        //接收参数
        $user_id = self::$uid;

        //判断贷款是否存在
        $user_loan_where['is_delete'] = IsDelete::No;
        $user_loan_where['is_history'] = IsHistory::not;
        $user_loan_where['user_id'] = $user_id;
        $user_loan_list = IDb::getInstance("asset_loan")->setDbWhere($user_loan_where)->setDbOrder("user_loan_id")->sel();
        if(empty($user_loan_list) || !is_array($user_loan_list)){
            $user_loan_list = array();
        }

        foreach ($user_loan_list as &$val){
            if($val['loan_type'] != LoanType::other && isset(HtmlEnumValue::$enum_value['loan_type'][$val['loan_type']])){
                $val['loan_type_name'] = HtmlEnumValue::$enum_value['loan_type'][$val['loan_type']];
            }
        }

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_loan_list",$user_loan_list);
        self::send();
    }

    /**
     * @功能：添加个人贷款
     * @开发者：gys
     */
    public function loan_add(){
        //接收参数
        $user_id = self::$uid;
        $loan_agent  = self::get_data('loan_agent',self::NOT_EMPTY,"贷款机构不能为空！");
        $loan_name  = self::get_data('loan_name',self::NOT_EMPTY,"贷款名称不能为空！");
        $loan_type  = self::get_data('loan_type',self::NOT_EMPTY,"贷款类型不能为空！");
        $loan_type_name  = self::get_data('loan_type_name');
        $loan_money  = self::get_data('loan_money',self::NOT_EMPTY,"贷款金额不能为空！");
        $loan_cycle  = self::get_data('loan_cycle',self::NOT_EMPTY,"贷款周期不能为空！");
        $repay_amount  = self::get_data('repay_amount',self::NOT_EMPTY,"已还款金额不能为空！");
        $loan_time  = self::get_data('loan_time',self::NOT_EMPTY,"贷款日期不能为空！");
        $settle_time  = self::get_data('settle_time',self::NOT_EMPTY,"结清日期不能为空！");

        if(strtotime($loan_time) >= strtotime($settle_time)){
            self::set_code(self::WARNING_0001);
            self::set_msg('结清日期必须大于贷款日期');
            self::send();
        }

        //长度限制
        $loan_money = self::data_filter($loan_money,self::INT,'贷款金额');
        $loan_cycle = self::data_filter($loan_cycle,self::INT_SMALL,'贷款周期');
        $repay_amount = self::data_filter($repay_amount,self::DOUBLE,'已还款金额');

        //判断是否设置空值
        if(trim($loan_time) == "") $loan_time = null;
        if(trim($settle_time) == "") $settle_time = null;

        if($loan_type == LoanType::other){
            $user_loan_data['loan_type_name'] = $loan_type_name;
        }else{
            $user_loan_data['loan_type_name'] = ['exp','null'];
        }

        //添加贷款
        $user_loan_data['user_id'] = $user_id;
        $user_loan_data['loan_agent'] = $loan_agent;
        $user_loan_data['loan_name'] = $loan_name;
        $user_loan_data['loan_type'] = $loan_type;
        $user_loan_data['loan_money'] = $loan_money;
        $user_loan_data['loan_cycle'] = $loan_cycle;
        $user_loan_data['repay_amount'] = $repay_amount;
        $user_loan_data['loan_time'] = $loan_time;
        $user_loan_data['settle_time'] = $settle_time;
        $user_loan_data['loan_status'] = DatumStatus::Wait;
        $user_loan_data['is_history'] = IsHistory::not;
        $user_loan_data['create_time'] = get_date_time();

        //开启事物
        IDb::dbStartTrans();

        //取得贷款列表
        $user_loan_add = IDb::getInstance("asset_loan")->setDbData($user_loan_data)->add();

        //判断添加贷款是否正常
        if($user_loan_add === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0001);
            self::set_msg("添加贷款信息失败！");
            self::send();
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_LOAN,IsLoan::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("添加贷款信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::set_value("user_loan_id",$user_loan_add);
        self::send();
    }

    /**
     * @功能：编辑个人贷款
     * @开发者：gys
     */
    public function loan_edit(){
        //接收参数
        $user_id = self::$uid;
        $user_loan_id  = self::get_data('user_loan_id',self::NOT_EMPTY,"贷款编号不能为空！");
        $loan_agent  = self::get_data('loan_agent',self::NOT_EMPTY,"贷款机构不能为空！");
        $loan_name  = self::get_data('loan_name',self::NOT_EMPTY,"贷款名称不能为空！");
        $loan_type  = self::get_data('loan_type',self::NOT_EMPTY,"贷款类型不能为空！");
        $loan_type_name  = self::get_data('loan_type_name');
        $loan_money  = self::get_data('loan_money',self::NOT_EMPTY,"贷款金额不能为空！");
        $loan_cycle  = self::get_data('loan_cycle',self::NOT_EMPTY,"贷款周期不能为空！");
        $repay_amount  = self::get_data('repay_amount');
        $loan_time  = self::get_data('loan_time');
        $settle_time  = self::get_data('settle_time');

        if(strtotime($loan_time) >= strtotime($settle_time)){
            self::set_code(self::WARNING_0001);
            self::set_msg('结清日期必须大于贷款日期');
            self::send();
        }

        //长度限制
        $loan_money = self::data_filter($loan_money,self::INT,'贷款金额');
        $loan_cycle = self::data_filter($loan_cycle,self::INT_SMALL,'贷款周期');
        $repay_amount = self::data_filter($repay_amount,self::DOUBLE,'已还款金额');

        //判断是否设置空值
        if(trim($loan_time) == "") $loan_time = null;
        if(trim($settle_time) == "") $settle_time = null;

        if($loan_type == LoanType::other){
            $user_loan_data['loan_type_name'] = $loan_type_name;
        }else{
            $user_loan_data['loan_type_name'] = ['exp','null'];
        }

        //添加贷款
        $user_loan_data['loan_agent'] = $loan_agent;
        $user_loan_data['loan_name'] = $loan_name;
        $user_loan_data['loan_type'] = $loan_type;
        $user_loan_data['loan_money'] = $loan_money;
        $user_loan_data['loan_cycle'] = $loan_cycle;
        $user_loan_data['repay_amount'] = $repay_amount;
        $user_loan_data['loan_time'] = $loan_time;
        $user_loan_data['settle_time'] = $settle_time;
        $user_loan_data['loan_status'] = DatumStatus::Wait;
        $user_loan_data['is_history'] = IsHistory::not;

        //取得贷款列表
        $user_loan_where['user_loan_id'] = $user_loan_id;
        $user_loan_info = IDb::getInstance("asset_loan")->setDbWhere($user_loan_where)->row();

        //判断是否审核通过
        if(empty($user_loan_info)){
            self::set_code(self::WARNING_0001);
            self::set_msg("修改贷款信息失败！");
            self::send();
        }

        //开启事物
        IDb::dbStartTrans();

        //判断是否审核通过
        if($user_loan_info['loan_status'] == DatumStatus::OK) {
            //取得贷款列表
            $user_loan_where = null;
            $user_loan_where['user_loan_id'] = $user_loan_id;
            $user_update_data['is_history'] = IsHistory::yes;
            $user_update_data['update_time'] = get_date_time();
            $user_loan_upd = IDb::getInstance("asset_loan")->setDbWhere($user_loan_where)->setDbData($user_update_data)->upd();
            if($user_loan_upd === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0002);
                self::set_msg("修改贷款信息失败！");
                self::send();
            }

            //添加贷款
            $user_loan_data['create_time'] = get_date_time();
            $user_loan_add = IDb::getInstance("asset_loan")->setDbData($user_loan_data)->add();
            if($user_loan_add === false){
                //数据回滚
                IDb::dbRollback();

                //返回错误
                self::set_code(self::WARNING_0003);
                self::set_msg("修改贷款信息失败！");
                self::send();
            }
        }else {
            //添加贷款
            $user_loan_where = null;
            $user_loan_where['user_loan_id'] = $user_loan_id;
            $user_loan_upd = IDb::getInstance("asset_loan")->setDbWhere($user_loan_where)->setDbData($user_loan_data)->upd();

            //判断修改贷款是否正常
            if ($user_loan_upd === false) {
                IDb::dbRollback();
                self::set_code(self::WARNING_0004);
                self::set_msg("修改贷款信息失败！");
                self::send();
            }
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_LOAN,IsLoan::Yes);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0005);
            self::set_msg("修改贷款信息失败！");
            self::send();
        }

        //事物提交
        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }

    /**
     * @功能：删除个人贷款
     * @开发者：gys
     */
    public function loan_del(){
        //接收参数
        $user_id = self::$uid;
        $user_loan_id  = self::get_data('user_loan_id',self::NOT_EMPTY,"贷款编码不能为空！");

        //添加贷款
        $user_loan_data['is_delete'] = IsDelete::Yes;
        $user_loan_data['update_time'] = get_date_time();

        //取得贷款列表
        $user_loan_where['user_id'] = $user_id;
        $user_loan_where['user_loan_id'] = $user_loan_id;

        IDb::dbStartTrans();
        $user_loan_upd = IDb::getInstance("asset_loan")->setDbWhere($user_loan_where)->setDbData($user_loan_data)->upd();

        //判断修改贷款是否正常
        if($user_loan_upd === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0002);
            self::set_msg("删除贷款信息失败！");
            self::send();
        }

        //同步个人信息
        $result = User::syncMaterialToUserInfo($user_id,User::MATERIAL_LOAN);
        if($result === false){
            IDb::dbRollback();
            self::set_code(self::WARNING_0003);
            self::set_msg("同步贷款信息失败！");
            self::send();
        }

        IDb::dbCommit();

        //操作成功
        self::set_code(self::SUCCESS);
        self::set_msg("成功！");
        self::send();
    }
}
